package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.service.VipService;
import com.zhentao.vo.UserVipInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * VIP控制器
 */
@RestController
@RequestMapping("/api/vip")
@CrossOrigin(origins = "*")
public class VipController {
    
    @Autowired
    private VipService vipService;
    
    /**
     * 获取VIP信息和套餐列表
     * @param userId 用户ID（可选，不传则默认为1）
     * @return VIP信息
     */
    @GetMapping("/info")
    public Result<UserVipInfoVO> getVipInfo(@RequestParam(required = false) Long userId) {
        try {
            // 如果没有传userId，默认使用1
            if (userId == null) {
                userId = 1L;
            }
            
            UserVipInfoVO vo = vipService.getVipInfo(userId);
            return Result.success(vo);
        } catch (Exception e) {
            return Result.error("获取VIP信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 购买VIP
     * @param userId 用户ID
     * @param packageId 套餐ID
     * @return 购买结果
     */
    @PostMapping("/purchase")
    public Result<String> purchaseVip(@RequestParam Long userId, @RequestParam Long packageId) {
        try {
            Boolean success = vipService.purchaseVip(userId, packageId);
            if (success) {
                return Result.success("VIP开通成功！", "success");
            } else {
                return Result.error("VIP开通失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("VIP开通失败");
        }
    }
}


